package com.kelvin.paopao.module.person.detail;

import com.kelvin.kelvinlib.db.dao.DaoSession;
import com.kelvin.kelvinlib.db.dao.UserInfoDao;
import com.kelvin.kelvinlib.db.table.UserInfo;
import com.kelvin.kelvinlib.utils.ToastUtil;
import com.kelvin.paopao.api.RetrofitService;
import com.kelvin.paopao.api.responseBean.CommonListResponse;
import com.kelvin.paopao.api.responseBean.PersonPhotoModel;
import com.kelvin.paopao.api.responseBean.UserDetailModel;
import com.kelvin.paopao.api.responseBean.UserGz;

import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;

/**
 * Created by MCK on 2017/5/9.
 */

public class PersonDetailPresenter implements IPersonDetailPresenter {

    private PersonDetailActivity mView;
    private DaoSession daoSession;

    /**
     * 是否关注 0是未关注 1是已关注
     */
    private int isFocus = -1;

    public PersonDetailPresenter(PersonDetailActivity view, DaoSession daoSession) {
        this.mView = view;
        this.daoSession = daoSession;
    }

    @Override
    public void getData(boolean isRefresh) {

    }

    @Override
    public void getMoreData() {

    }

    public void focus(String user_no) {
        RetrofitService.focus(user_no).subscribe(new Consumer<CommonListResponse>() {
            @Override
            public void accept(@NonNull CommonListResponse commonListResponse) throws Exception {
                if (commonListResponse.getReason().equals("成功")) {
                    ToastUtil.showToast(mView, "关注成功");
                    isFocus = 1;
                    mView.setFocus();
                }
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(@NonNull Throwable throwable) throws Exception {

            }
        }, new Action() {
            @Override
            public void run() throws Exception {

            }
        });
    }

    public void unfocus(String user_no) {
        RetrofitService.unfocus(user_no).subscribe(new Consumer<CommonListResponse>() {
            @Override
            public void accept(@NonNull CommonListResponse commonListResponse) throws Exception {
                if (commonListResponse.getReason().equals("成功")) {
                    ToastUtil.showToast(mView, "取消关注成功");
                    mView.setUnfocus();
                    isFocus = 1;
                }
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(@NonNull Throwable throwable) throws Exception {

            }
        }, new Action() {
            @Override
            public void run() throws Exception {

            }
        });
    }

    @Override
    public void changefocus(String user_no) {
        if (isFocus == 0) {
            focus(user_no);
        } else if (isFocus == 1) {
            unfocus(user_no);
        }
    }

    @Override
    public void getUsetDetial(String user_no) {
        RetrofitService.getUserInfo(user_no)
                .subscribe(new Consumer<CommonListResponse<UserDetailModel>>() {
                    @Override
                    public void accept(@NonNull CommonListResponse<UserDetailModel> userDetailModelCommonListResponse) throws Exception {
                        UserDetailModel userDetailModel = userDetailModelCommonListResponse
                                .getResult().get(0);
                        if (userDetailModelCommonListResponse.getReason().equals("成功") && userDetailModel != null) {
                            mView.loadData(userDetailModelCommonListResponse.getResult().get(0));
                            UserInfoDao userInfoDao = daoSession.getUserInfoDao();
                            UserInfo userInfo = new UserInfo(Long.parseLong(userDetailModel.getUser_no()), userDetailModel.getUser_name(),
                                    userDetailModel.getUser_headimg(), null);
                            userInfoDao.insert(userInfo);
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(@NonNull Throwable throwable) throws Exception {

                    }
                }, new Action() {
                    @Override
                    public void run() throws Exception {

                    }
                });
    }

    @Override
    public void checkFocus(String user_no) {
        RetrofitService.checkFocus(user_no).subscribe(new Consumer<CommonListResponse<UserGz>>() {
            @Override
            public void accept(@NonNull CommonListResponse<UserGz> commonListResponse) throws Exception {
                isFocus = commonListResponse.getResult().get(0).getGz();
                if (isFocus == 1) {
                    mView.setFocus();
                } else {
                    mView.setUnfocus();
                }
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(@NonNull Throwable throwable) throws Exception {

            }
        }, new Action() {
            @Override
            public void run() throws Exception {

            }
        });
    }

    @Override
    public void getUserPhoto(String user_no) {
        RetrofitService.getPersonPhoto(user_no).subscribe(new Consumer<CommonListResponse<PersonPhotoModel>>() {
            @Override
            public void accept(@NonNull CommonListResponse<PersonPhotoModel> personPhotoModelCommonListResponse) throws Exception {
                if (personPhotoModelCommonListResponse.getReason().equals("成功")) {
                    mView.loadPhoto(personPhotoModelCommonListResponse.getResult());
                }
            }
        });
    }
}
